define(['jquery', 'bootstrap', 'backend', 'table', 'form','upload','editable'], function ($, undefined, Backend, Table, Form, Upload)  {


    if(Config.accounturl == '' || Config.accounturl == null){
        Config.accounturl = 0;
    }
    if(Config.daystamp == '' || Config.daystamp == null){
        Config.daystamp = 0;
    }
    if(Config.gateway == '' || Config.gateway == null){
        Config.gateway = 0;
    }
    if(Config.type == '' || Config.type == null){
        Config.type = 0;
    }
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'feditionordershopify/index/?accounturl=' + Config.accounturl + '&daystamp=' + Config.daystamp + '&gateway=' + Config.gateway + '&type=' + Config.type,
                    add_url: 'feditionordershopify/add',
                    edit_url: 'feditionordershopify/edit',
                    del_url: 'feditionordershopify/del',
                    multi_url: 'feditionordershopify/multi',
                    import_url: 'feditionordershopify/import',
                    table: 'fedition_ordershopify',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'updated_at',
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: 'LIKE'},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        // {field: 'number', title: __('Number'), operate: 'LIKE'},
                        // {field: 'order_number', title: __('Order_number'), operate: 'LIKE'},
                        {field: 'accounturl', title: __('Account_address'),visible: false,addClass: "selectpage", extend: "data-source='feditionaccounturl/index' data-field='name'"},
                        {field: 'accounturl_name', title: __('Account_address'), operate: false},
                        {field: 'created_at', title: __('Created_at'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updated_at', title: __('Updated_at'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'ytotal_price', title: __('总价'), operate: 'LIKE'},
                        {field: 'total_price', title: __('更新价格'), operate: 'LIKE'},
                        {field: 'referring_site', title: __('来源网址'), formatter:Table.api.formatter.flag, operate: 'LIKE'},
                        {field: 'landing_site', title: __('来源网址2'), formatter:Table.api.formatter.flag, operate: 'LIKE'},
                        {field: 'tags', title: __('tags'), formatter:Table.api.formatter.flag, operate: 'LIKE'},
                        {field: 'user_id', title: __('商品归属人'), formatter:Table.api.formatter.flag, operate: 'LIKE'},
                        {field: 'currency', title: __('Currency'), operate: 'LIKE'},
                        {field: 'orderquantity_title', title: __('商品名称'), operate: false},
                        {field: 'orderquantity_sku', title: __('商品SKU'), operate: false},
                        {field: 'orderquantity_quantitys', title: __('购买数量'), operate: false},
                        {field: 'province', title: __('省份'), operate: false},
                        {field: 'zip', title: __('邮编'), operate: false},
                        {field: 'note', title: __('是否争议'), operate: 'LIKE'},
                        // {field: 'tracking_company', title: __('TrackingCompany'), operate: 'LIKE'},
                        // {field: 'tracking_number', title: __('TrackingNumber'), operate: 'LIKE'},
                        {field: 'gateway', title: __('支付方式'), formatter:Table.api.formatter.flag, operate: 'LIKE',custom:{'shopify_payments':'info','paypal':'warning','shopify_installments':'info'}},


                        {field: 'financial_status', title: '订单支付状态', operate: false,
                            editable: {
                            type: 'select',
                            pk: 1,
                            source:
                                    [
                                        {value: 'paid', text: '已付款'},
                                        {value: 'refunded', text: '已退款'},
                                        {value: 'partially_refunded', text: '部分退款'},
                                    ]
                            },formatter: function (value, row) {
                                return row.financial_status;
                            }
                        },

                        // ,"any":__('所有订单'),"unpaid":__('授权和部分支付'),"voided":__('无效'),"partially_refunded":__('部分退款'),"partially_paid":__('部分支付'),"authorized":__('授权订单'),"pending":__('挂单'),
                        {field: 'fulfillment_status', title: __('发货状态'), searchList: {"fulfilled":__('已发货'),"unfulfilled":__('未发货'),"":__('未完成')}, formatter: Table.api.formatter.status,custom: {unfulfilled:'danger', fulfilled:'success'}},
                        // "partial":__('部分发货'),"unshipped":__('尚未发货'),"any":__('所有发货状态'),"shipped":__('已完成')

                        {field: 'email', title: __('邮箱'), operate: 'LIKE'},
                        {field: 'shipping_name', title: __('购买人姓名'), operate: 'LIKE'},
                        {field: 'phone', title: __('联系方式'), operate: 'LIKE'},
                        {field: 'country_code', title: __('国家'), operate: 'LIKE'},
                        {field: 'city', title: __('城市'), operate: 'LIKE'},
                        {field: 'address1', title: __('地址'), operate: 'LIKE'},

                        {field: 'tracking_company', title: __('运货公司'), operate: 'LIKE'},
                        {field: 'tracking_number', title: __('运单号'), operate: 'LIKE'},
                        {field: 'freightmj', title: __('运费成本（美金）'), operate: 'BETWEEN'},
                        {field: 'material', title: __('采购成本（美金）'),  operate: 'BETWEEN'},
                        // {field: 'gross_profit', title: __('毛利（美金）'), operate: 'BETWEEN'},
                        //{field: 'istransfer', title: __('运费成本状态'), searchList: {"0":__('未对接'),"1":__('对接成功'),"3":__('对接接口无数据'),"5":__('手动添加完成')}, formatter: Table.api.formatter.status,custom: {3:'danger', 1:'success', 0:'warning', 5:'info'}},
                        //{field: 'isfiled', title: __('SKU状态'), searchList: {"0":__('未对接'),"1":__('对接成功'),"2":__('SKU跟商品都为空'),"3":__('对接接口无数据'),"4":__('SKU未填写'),"5":__('手动添加完成')}, formatter: Table.api.formatter.status,custom: {3:'danger', 1:'success', 2:'info', 0:'warning'}},
                        //{field: 'ismaterial', title: __('采购成本状态'), searchList: {"0":__('未对接'),"1":__('对接成功'),"3":__('对接接口无数据'),"4":__('手动添加完成')}, formatter: Table.api.formatter.status,custom: {3:'danger', 1:'success', 0:'warning', 4:'info'}},

                        {field: 'financial_status', title: __('Financial_status'), searchList: {"paid":__('已付款'),"refunded":__('已退款'),"partially_refunded":__('部分退款'),"partially_paid":__('部分支付'),"voided":__('无效')}, formatter: Table.api.formatter.status,custom: {refunded:'danger', paid:'success', partially_refunded:'warning', partially_paid:'warning', voided:'danger'}},

                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('订单详情'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('查看商品详情'),
                                    title: __('查看商品详情'),
                                    extend: 'data-area=\'["90%","90%"]\'',
                                    classname: 'btn btn-sm btn-success btn-dialog',
                                    icon: 'fa fa-try fa-fw',
                                    url: 'feditionorderquantity/index'
                                },
                                {
                                    name: 'detail',
                                    text: __('查看物流详情'),
                                    title: __('查看物流详情'),
                                    extend: 'data-area=\'["90%","90%"]\'',
                                    classname: 'btn btn-sm btn-info btn-dialog',
                                    icon: 'fa fa-try fa-fw',
                                    url: 'feditionorderfulfillments/index'
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('获取商品最新SKU'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('获取商品最新SKU'),
                                    title: __('获取商品最新SKU'),
                                    classname: 'btn btn-sm btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-usd',
                                    confirm: '确认更新吗？',
                                    url: 'feditionordershopify/addorderproduct',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //刷新父级页面
                                        // window.parent.location.reload();
                                        //刷新当前页面
                                        table.bootstrapTable('refresh', {});
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('订单采购成本'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('更新采购成本'),
                                    title: __('更新采购成本'),
                                    classname: 'btn btn-sm btn-info btn-magic btn-ajax',
                                    icon: 'fa fa-usd',
                                    confirm: '确认更新吗？',
                                    url: 'feditionordershopify/addmaterial',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //刷新父级页面
                                        // window.parent.location.reload();
                                        //刷新当前页面
                                        table.bootstrapTable('refresh', {});
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('更新订单'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('获取订单最新数据'),
                                    title: __('获取订单最新数据'),
                                    classname: 'btn btn-sm btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-usd',
                                    confirm: '确认更新吗？',
                                    url: 'feditionordershopify/updateordertime',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //刷新父级页面
                                        // window.parent.location.reload();
                                        //刷新当前页面
                                        table.bootstrapTable('refresh', {});
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                $("#zsum").text(data.Statistics.zsum);
                $("#zfreightmj").text(data.Statistics.zfreightmj);
                $("#zmaterial").text(data.Statistics.zmaterial);
                $("#zcountnum").text(data.Statistics.zcountnum);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        doubt: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'feditionordershopify/doubt',
                    add_url: 'feditionordershopify/add',
                    edit_url: 'feditionordershopify/edit',
                    del_url: 'feditionordershopify/del',
                    multi_url: 'feditionordershopify/multi',
                    import_url: 'feditionordershopify/import',
                    table: 'fedition_ordershopify',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'updated_at',
                search:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'accounturl', title: __('Account_address'),visible: false,addClass: "selectpage", extend: "data-source='feditionaccounturl/index' data-field='name'"},
                        {field: 'accounturl_name', title: __('Account_address'), operate: false},
                        {field: 'created_at', title: __('Created_at'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updated_at', title: __('Updated_at'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'ytotal_price', title: __('原价'), operate: 'LIKE'},
                        {field: 'total_price', title: __('当前价格'), operate: 'LIKE'},
                        // {field: 'gateway', title: __('支付方式'), formatter:Table.api.formatter.flag, operate: 'LIKE',custom:{'shopify_payments':'info','paypal':'warning','shopify_installments':'info'}},
                        {field: 'financial_status', title: __('Financial_status'), searchList: {"paid":__('已付款'),"refunded":__('已退款'),"partially_refunded":__('部分退款'),"partially_paid":__('部分支付'),"voided":__('无效')}, formatter: Table.api.formatter.status,custom: {refunded:'danger', paid:'success', partially_refunded:'warning', partially_paid:'warning', voided:'danger'}},
                        {field: 'fulfillment_status', title: __('发货状态'), searchList: {"fulfilled":__('已发货'),"unfulfilled":__('未发货'),"":__('未完成')}, formatter: Table.api.formatter.status,custom: {unfulfilled:'danger', fulfilled:'success'}},
                        // {field: 'tracking_company', title: __('运货公司'), operate: false},
                        // {field: 'tracking_number', title: __('运单号'), operate: false},
                        // {field: 'freightmj', title: __('运费成本（美金）'), operate: 'BETWEEN'},
                        {field: 'material', title: __('采购成本（美金）'), editable: true, operate: 'BETWEEN'},
                        {field: 'ismaterial', title: __('采购成本状态'), searchList: {"0":__('未对接'),"1":__('对接成功'),"3":__('对接接口无数据'),"4":__('手动添加完成')}, formatter: Table.api.formatter.status,custom: {3:'danger', 1:'success', 0:'warning', 4:'info'}},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('订单详情'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('查看商品详情'),
                                    title: __('查看商品详情'),
                                    extend: 'data-area=\'["90%","90%"]\'',
                                    classname: 'btn btn-sm btn-info btn-dialog',
                                    icon: 'fa fa-try fa-fw',
                                    url: 'feditionorderquantity/index'
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('获取商品最新SKU'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('获取商品最新SKU'),
                                    title: __('获取商品最新SKU'),
                                    classname: 'btn btn-sm btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-usd',
                                    confirm: '确认更新吗？',
                                    url: 'feditionordershopify/addorderproduct',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //刷新父级页面
                                        // window.parent.location.reload();
                                        //刷新当前页面
                                        table.bootstrapTable('refresh', {});
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('订单采购成本'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('更新采购成本'),
                                    title: __('更新采购成本'),
                                    classname: 'btn btn-sm btn-info btn-magic btn-ajax',
                                    icon: 'fa fa-usd',
                                    confirm: '确认更新吗？',
                                    url: 'feditionordershopify/addmaterial',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //刷新父级页面
                                        // window.parent.location.reload();
                                        //刷新当前页面
                                        table.bootstrapTable('refresh', {});
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('更新订单'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('获取订单最新数据'),
                                    title: __('获取订单最新数据'),
                                    classname: 'btn btn-sm btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-usd',
                                    confirm: '确认更新吗？',
                                    url: 'feditionordershopify/updateordertime',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //刷新父级页面
                                        // window.parent.location.reload();
                                        //刷新当前页面
                                        table.bootstrapTable('refresh', {});
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'ismaterial',title: __('标记为正常'),formatter: Table.api.formatter.toggle,yes: '1',no: '0'},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});